package com.gxa.service.commserve.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dto.commservedto.LostFoundSearchCriteria;
import com.gxa.dto.commservedto.RecruitmentRecordDto;
import com.gxa.entity.commserve.LostFound;
import com.gxa.mapper.commserve.LostFoundMapper;
import com.gxa.mapper.commserve.RecruitmentRecordMapper;
import com.gxa.service.commserve.LostFoundService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:18:00
 */
@Service
public class LostFoundServiceImpl extends ServiceImpl<LostFoundMapper, LostFound> implements LostFoundService {
   @Autowired
   private LostFoundMapper lostFoundMapper;

   @Autowired
   private RecruitmentRecordMapper recruitmentRecordMapper;

    @Override
    public void addLostFound(LostFound lostFound) {

        long l = System.currentTimeMillis();
        Date date = new Date(l);
        lostFound.setReleaseTime(date);

        this.lostFoundMapper.insert(lostFound);
        Integer lfId = lostFound.getLfId();

        RecruitmentRecordDto recruitmentRecordDto = new RecruitmentRecordDto();
        recruitmentRecordDto.setOperator(lostFound.getPublisher());
        recruitmentRecordDto.setOperation(0);
        recruitmentRecordDto.setOperationTime(date);
        recruitmentRecordDto.setReleaseTime(date);
        recruitmentRecordDto.setLostFoundId(lfId);

        this.recruitmentRecordMapper.addRecruitmentRecord(recruitmentRecordDto);

    }

    @Override
    public void updateState(LostFound lostFound) {
        LostFound lostFound1 = new LostFound();
        lostFound1.setLfId(lostFound.getLfId());
        lostFound1.setState(lostFound.getState());
        UpdateWrapper<LostFound> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("lf_id",lostFound1.getLfId());
        updateWrapper.set("state",lostFound1.getState());
        this.lostFoundMapper.update(lostFound1,updateWrapper);

        long l = System.currentTimeMillis();
        Date date = new Date(l);
        RecruitmentRecordDto recruitmentRecordDto = new RecruitmentRecordDto();
        recruitmentRecordDto.setLostFoundId(lostFound.getLfId());
        recruitmentRecordDto.setOperator(lostFound.getPublisher());
        recruitmentRecordDto.setOperation(1);
        recruitmentRecordDto.setOperationTime(date);
        this.recruitmentRecordMapper.addRecruitmentRecord(recruitmentRecordDto);

    }

    @Override
    public Map<String, Object> queryList(Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashedMap();
        QueryWrapper<LostFound> queryWrapper = new QueryWrapper<>();
        Page<LostFound> page = PageHelper.startPage(pageNum,pageSize);
        List<LostFound> lostFoundList = this.lostFoundMapper.selectList(queryWrapper);
        PageInfo<LostFound> pageInfo = new PageInfo<>(lostFoundList);
        map.put("pageInfo",pageInfo);
        return map;
    }

    @Override
    public Map<String, Object> queryListBySearch(LostFoundSearchCriteria lostFoundSearchCriteria, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashedMap();
        QueryWrapper<LostFound> queryWrapper = new QueryWrapper<>();
        if (lostFoundSearchCriteria.getState()!=null){
            queryWrapper.like("state",lostFoundSearchCriteria.getState());
        }
        if ((lostFoundSearchCriteria.getStartTime()!=null&&!lostFoundSearchCriteria.getStartTime().equals("")) || (lostFoundSearchCriteria.getEndTime()!=null&&!lostFoundSearchCriteria.equals(""))){
            queryWrapper.between("release_time",lostFoundSearchCriteria.getStartTime(),lostFoundSearchCriteria.getEndTime());
        }
        Page<LostFound> page = PageHelper.startPage(pageNum,pageSize);
        List<LostFound> lostFoundList = this.lostFoundMapper.selectList(queryWrapper);
        PageInfo<LostFound> pageInfo = new PageInfo<>(lostFoundList);
        map.put("pageInfo",pageInfo);
        return map;
    }

    @Override
    public LostFound queryById(Integer id) {
        LostFound lostFound = this.lostFoundMapper.selectById(id);
        return lostFound;
    }

    @Override
    public LostFound queryByDate(Date date) {
        QueryWrapper<LostFound> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("release_time",date);
        this.lostFoundMapper.selectOne(queryWrapper);
        return null;
    }
}
